package com.wontube.smr.bo.sites;

import java.io.IOException;

import org.dom4j.DocumentException;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.smr.bo.constants.SiteConstants;
import com.wontube.smr.bo.sites.analizers.YouTubeVideoAnalizer;
import com.wontube.smr.bo.sites.youtube.feed.YouTubeFeedsBO;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.po.VVideo;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.client.model.vo.SearchCountPreview;
import com.wontube.smr.client.model.vo.VVideoItems;
import com.wontube.smr.dao.bo.MovieSelectDao;
import com.wontube.smr.dao.bo.TVSelectDao;
import com.wontube.smr.dao.bo.VideoSelectDao;


/**
 * @author yuechao
 *
 */
public final class VideoSelectBO {	
	private final static String SEARCH_TYPE_I18N = "I18N";
	private final static String SEARCH_TYPE_MOBILE = "Mobile";
	private final static String SEARCH_TYPE_WEB = "Web";
	
	public static VVideoItems searchAllVideosForMobile(double dbFactor, String keyword, PagenateInfo pageInfo){
		return searchAllVideosByType(SEARCH_TYPE_MOBILE, dbFactor, keyword, pageInfo);
	}
	
	/**
	 * exclude Crunchyroll
	 * @param dbFactor
	 * @param keyword
	 * @param pageInfo
	 * @return
	 */
	public static VVideoItems searchAllVideosForWeb(double dbFactor, String keyword, PagenateInfo pageInfo){
		return searchAllVideosByType(SEARCH_TYPE_WEB, dbFactor, keyword, pageInfo);
	}
	
	public static VVideoItems searchAllVideosForI18n(double dbFactor, String keyword, PagenateInfo pageInfo){
		return searchAllVideosByType(SEARCH_TYPE_I18N, dbFactor, keyword, pageInfo);
	}
	
	private static VVideoItems searchAllVideosByType(String type, double dbFactor, String keyword, PagenateInfo pageInfo){
		VVideoItems items = new VVideoItems(pageInfo);
		if(dbFactor > 1 || dbFactor < 0){
			dbFactor = 1;
		}
		
		// 优先展示资源库的内容
		int dbPageSize = (int)(pageInfo.getPageSize() * dbFactor);
		int dbSize = 0;
		if(dbPageSize > 0){
			PagenateInfo dbPageInfo = new PagenateInfo(pageInfo.getPageNo(), dbPageSize);
			items = searchVideosByType(type, keyword, dbPageInfo);
			dbSize = items.getItems().size();
		}
		
		int ybPageSize = pageInfo.getPageSize() - dbSize;
		
		if (ybPageSize > 0) {
			PagenateInfo feedsPageInfo = new PagenateInfo(pageInfo.getPageNo(), ybPageSize);			
			MovieItems feedsItems = null;
			try {
				feedsItems = YouTubeFeedsBO.searchVideoFeeds(keyword, null, feedsPageInfo);
			} catch (Exception e) {				
			} 
			
			int count = 0;
			if(feedsItems != null){
				count = feedsItems.getItems().size();
			}
			if(count < ybPageSize && dbSize == dbPageSize){
				//重新db
				dbPageSize += ybPageSize - count;
				PagenateInfo dbPageInfo = new PagenateInfo(pageInfo.getPageNo(), dbPageSize);
				items = searchVideosByType(type, keyword, dbPageInfo);			
			}
			
			if(feedsItems != null){
				for (Movie movie : feedsItems.getItems()) {
					VVideo item = toVVideo(movie);
					items.getItems().add(item);
				}	
			}			
		}
		
		return items;
	}
	
	private static VVideoItems searchVideosByType(String type, String keyword, PagenateInfo pageInfo){
		if(SEARCH_TYPE_I18N.equals(type)){
			return VideoSelectDao.searchVVideosForI18n(keyword, pageInfo);	
		}else if(SEARCH_TYPE_MOBILE.equals(type)){
			return VideoSelectDao.searchVVideosForMobile(keyword, pageInfo);
		}else{
			return VideoSelectDao.searchVVideosForWeb(keyword, pageInfo);		
		}
	}
	
	public static final String getThumbnail(final String url) throws IOException, DocumentException {
		String videoId = YouTubeVideoAnalizer.getVideoId(url);
		if ("".equals(videoId)) { // not youtube
			String thumbnail = VideoSelectDao.getThumbnail(url);
			return thumbnail;
		} 
		
		// youtube
		Movie movie = YouTubeFeedsBO.getSingle(videoId);
		return movie.getThumbnail();
	}
	
	public static final SearchCountPreview previewItemsCount(String keyword) {
		SearchCountPreview preview = new SearchCountPreview();
		int showCount = TVSelectDao.getHuluTVShowCount(keyword);
		preview.setShowCount(showCount);		
		
		int movieCount = MovieSelectDao.getMovieCount(keyword, Movie.TYPE_MOVIE, 
				SiteConstants.WEBSITE_HULU);
		
		preview.setMovieCount(movieCount);
		
		int musicCount = MovieSelectDao.getMovieCount(keyword, Movie.TYPE_MUSIC, 
				SiteConstants.WEBSITE_VEVO);
		preview.setMvCount(musicCount);
		
		int trailerCount = MovieSelectDao.getMovieCount(keyword, Movie.TYPE_TRAILER);
		preview.setTrailerCount(trailerCount);
		
		
		PagenateInfo feedsPageInfo = new PagenateInfo(1, 0);		
		MovieItems movieItems = YouTubeFeedsBO.searchVideoFeeds(keyword, null, feedsPageInfo);
		preview.setYoutubeCount(movieItems.getTotalCount());	
	
		return preview;
	}
	
	public static final SearchCountPreview previewI18nItemsCount(String keyword) {
		SearchCountPreview preview = new SearchCountPreview();
		
		//youtube movie
		int movieCount = MovieSelectDao.getMovieCount(keyword, Movie.TYPE_MOVIE, 
				SiteConstants.WEBSITE_YOUTUBE);		
		preview.setMovieCount(movieCount);
		
		//youtube music
		int musicCount = MovieSelectDao.getMovieCount(keyword, Movie.TYPE_MUSIC, 
				SiteConstants.WEBSITE_YOUTUBE);
		preview.setMvCount(musicCount);
		
		//Imdb trailer
		int trailerCount = MovieSelectDao.getMovieCount(keyword, Movie.TYPE_TRAILER, 
				SiteConstants.WEBSITE_IMDB);
		preview.setTrailerCount(trailerCount);
		
		//crunchyroll anime
		int animeCount = TVSelectDao.getCrunchyrollAnimeCount(keyword);
		preview.setAnimeCount(animeCount);
		
		//youtube short film
		PagenateInfo feedsPageInfo = new PagenateInfo(1, 0);		
		MovieItems movieItems = YouTubeFeedsBO.searchVideoFeeds(keyword, null, feedsPageInfo);
		preview.setYoutubeCount(movieItems.getTotalCount());	
	
		return preview;
	}
	
	/**
	 * @param movie
	 * @return
	 */
	private static VVideo toVVideo(Movie movie) {
		VVideo item = new VVideo();
		item.setId(movie.getId());
		item.setActors(movie.getActors());
		item.setDescription(movie.getDescription());
		item.setDirector(movie.getDirector());
		item.setDuration(movie.getDuration());
		item.setGenre(movie.getGenre());
		item.setItemUrl(movie.getItemUrl());
		item.setParentTitle("");
		item.setAirBeginDate(movie.getPublishTime());
		item.setAirEndDate(null);
		item.setScore(movie.getScore());
		item.setThumbnail(movie.getThumbnail());
		item.setTitle(movie.getTitle());
		item.setType(movie.getMovieType());
		item.setVersion(movie.getVersion());
		item.setWebsite(movie.getWebsite());
		
		return item;
	}
}
